import 'dart:math';
import 'dart:ui';

import 'package:flutter/material.dart';

class BasicPaintPage extends StatelessWidget {
  const BasicPaintPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      child: null,
      painter: BasicPainter(),
    );
  }
}

class BasicPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    canvas.drawColor(Color(0xFF101010), BlendMode.color);
    var center = size / 2;
    var paint = Paint()..color = Colors.yellow[100]!;
    paint.strokeWidth = 2.0;

    canvas.drawCircle(
      Offset(center.width, center.height),
      90,
      paint,
    );
    canvas.drawLine(
      Offset(0, size.height * 3 / 4),
      Offset(size.width, size.height * 3 / 4),
      paint,
    );

    var style = TextStyle(
      fontWeight: FontWeight.bold,
      fontSize: 30.0,
      color: Colors.yellow[100],
    );

    final ParagraphBuilder paragraphBuilder = ParagraphBuilder(
      ParagraphStyle(
        fontSize: style.fontSize,
        fontFamily: style.fontFamily,
        fontStyle: style.fontStyle,
        fontWeight: style.fontWeight,
        textAlign: TextAlign.center,
      ),
    )
      ..pushStyle(style.getTextStyle())
      ..addText('海上升明月 天涯共此时');
    final Paragraph paragraph = paragraphBuilder.build()
      ..layout(ParagraphConstraints(width: size.width));
    canvas.drawParagraph(paragraph, Offset(0, 100));

    // 向中心点闭合的弧
    canvas.drawArc(
      Rect.fromCenter(
        center: Offset(center.width - 100, size.height - 80),
        width: 50,
        height: 50,
      ),
      0.4,
      2 * pi - 0.8,
      false,
      paint,
    );
    // 端点闭合的弧
    canvas.drawArc(
      Rect.fromCenter(
        center: Offset(center.width + 100, size.height - 80),
        width: 50,
        height: 50,
      ),
      0.4,
      2 * pi - 0.8,
      true,
      paint,
    );

    var path = Path()
      ..moveTo(40, 40)
      ..lineTo(100, 100)
      ..lineTo(40, 100)
      ..lineTo(40, 40);
    canvas.drawPath(path, paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}
